#!/bin/bash

#保存到Git仓库顶层目录

GIT_FILE_BASE="."
GIT_INC_FILE=
GIT_SRC_FILE=

#脚本中间变量
# 最新的提交前16位
git_commit=
# 编译所在分支名
git_branch=
# 最新的提交的文字说明
git_topic=
# 最新的提交的日期
git_date=
# 最新的提交的仓库状态(代码是否有未提交到本地，提交是否有未推送到服务器)
git_status=
# 最新的提交的作者
git_author=
# 编译程序的用户名
compiler=

function git_version_file_create() {
    GIT_INC_FILE=$GIT_FILE_BASE/git_version_autogen.h
    GIT_SRC_FILE=$GIT_FILE_BASE/git_version_autogen.c

    #生成版本源文件
    echo "//ATUO GENERATE,DO NOT CHANGE"                                >  $GIT_SRC_FILE
    echo "#include \"git_version_autogen.h\""                           >> $GIT_SRC_FILE
    echo ""                                                             >> $GIT_SRC_FILE
    echo "char *git_commit  = \"$git_commit\";"                         >> $GIT_SRC_FILE
    echo "char *git_branch  = \"$git_branch\";"                         >> $GIT_SRC_FILE
    echo "char *git_topic   = \"$git_topic\";"                          >> $GIT_SRC_FILE
    echo "char *git_date    = \"$git_date\";"                           >> $GIT_SRC_FILE
    echo "char *git_status  = \"$git_status\";"                         >> $GIT_SRC_FILE
    echo "char *git_author  = \"$git_author\";"                         >> $GIT_SRC_FILE
    echo "char *compiler    = \"$compiler\";"                           >> $GIT_SRC_FILE
    echo ""                                                             >> $GIT_SRC_FILE
    echo "void print_git_version(int (*print)(const char *format, ...)) {"  >> $GIT_SRC_FILE 
    echo 	"    print(\"git commit : %s\r\n\",git_commit);"		        >> $GIT_SRC_FILE 
    echo 	"    print(\"git branch : %s\r\n\",git_branch);"		        >> $GIT_SRC_FILE 
    echo 	"    print(\"git topic : %s\r\n\",git_topic);"		            >> $GIT_SRC_FILE 
    echo 	"    print(\"git data : %s\r\n\",git_date);"			        >> $GIT_SRC_FILE 
    echo 	"    print(\"git status : %s\r\n\",git_status);"		        >> $GIT_SRC_FILE 
    echo 	"    print(\"git author : %s\r\n\",git_author);"		        >> $GIT_SRC_FILE 
    echo 	"    print(\"compiler : %s\r\n\",compiler);"			        >> $GIT_SRC_FILE 
    echo "}"									                            >> $GIT_SRC_FILE 

    #生成版本头文件
    echo "#ifndef __GIT_VERSION_AUTOGEN_H__"                                >  $GIT_INC_FILE
    echo "#define __GIT_VERSION_AUTOGEN_H__"                                >> $GIT_INC_FILE
    echo "//ATUO GENERATE,DO NOT CHANGE"                                    >> $GIT_INC_FILE
    echo ""                                                                 >> $GIT_INC_FILE
    echo "void print_git_version(int (*print)(const char *format, ...));"   >> $GIT_INC_FILE 
    echo ""                                                                 >> $GIT_INC_FILE
    echo "extern char *git_commit;"                                         >> $GIT_INC_FILE
    echo "extern char *git_branch;"                                         >> $GIT_INC_FILE
    echo "extern char *git_topic;"                                          >> $GIT_INC_FILE
    echo "extern char *git_date;"                                           >> $GIT_INC_FILE
    echo "extern char *git_status;"                                         >> $GIT_INC_FILE
    echo "extern char *git_author;"                                         >> $GIT_INC_FILE
    echo "extern char *compiler;"                                           >> $GIT_INC_FILE
    echo ""                                                                 >> $GIT_INC_FILE
    echo "#endif /* __GIT_VERSION_AUTOGEN_H__ */"                           >> $GIT_INC_FILE
}

function git_info_extract() {

    #获取最新git提交号
    git_commit=`git log -1 --format=%H | cut -c1-16`

    #获取分支名
    branch=`git rev-parse --abbrev-ref HEAD`
    git_branch=$branch

    #获取最新git提交描述
    git_topic=`git log -1  --pretty=%s`

    #获取最新提交的日期
    git_date=`git log -1 --pretty=%cd`

    #判断是否存在远程分支
    remote_exist=`git remote`
    if [ $? -eq 0 ]; then
        #存在远程分支
        unpush=`git log origin/$branch..$branch --oneline | wc -l`
        git_status=`git status -s | wc -l`" files uncommit,"$unpush" commit unpush"
    else
        #不存在远程分支
        git_status=`git status -s | wc -l`" files uncommit"
    fi

    #获取最新提交的作者
    git_author=`git log -1 --format='%an'`

    #编译该程序的用户名
    compiler=`whoami` 
    
    #根据提取的信息，创建版本文件
    git_version_file_create
    echo -e "\033[33m------自动生成版本文件git_version_autogen.c成功------\033[0m"
}

#检查git仓库是否存在
git status > /dev/null 2>&1
git_repo_exit=$?
#不存在Git仓库，则不用进行版本自动管理
if [ $git_repo_exit -eq 0 ]; then
    git_info_extract
fi